<?php 
	session_start();	

	if (!empty($_COOKIE['name'])) {
		$username = $_COOKIE['name'];
		$password = $_COOKIE['password'];
		login($username,$password);
	} 

		if(isset($_POST['submit'])){
			
			$username = trim($_POST['username']);
			$password = trim($_POST['password']);
			if (empty($username) || empty($_POST['password'])) {
				echo "用户名或者密码不能为空！";
			} else {
				login($username,$password);	
				if ($_POST['remeber'] == 'on') {
					 setcookie('name',$_SESSION['name'],time()+3600);
					 setcookie('password',$_SESSION['password'],time()+3600);					
				}					
			}		
		}else{
			if($_POST['logout']=='logout'){
				unset($_SESSION['name']);
				unset($_SESSION['password']);
				setcookie('name','');
				setcookie('password','');
				header("location:login.html");
			}
			//echo "请点击<a href='login.html'>登录</a>";
			//header("location:login.html");
		}

	function login($username,$pass)
	{
		$password = md5($pass);
		include_once("shuju.php");
		if ($_POST['login'] == 'user') {
			$sql = "select * from user where name = '$username' and password = '$password'";
				$db = new DB();
				$user = $db->get($sql);
				$del = $user[0][5];
				//echo "<pre>";
				//var_dump($user);
				if (empty($user)) {
					echo "该用户不存在或者用户密码错误！";
				} else {
					if ($del == 0) {
						echo "<script>alert('用户审核未通过！');location.href='login.html';</script>";
					}else{
					//echo $user[0][1]."用户已登录！";	
					echo session_id();
					$_SESSION['name'] = $username;
					$_SESSION['password'] = $pass;
					$_SESSION['id'] = $user[0][0];
					header("location:customer-myguest.php");}
				}
		 }else{
			$sql = "select * from admin where name = '$username' and password = '$password'";
				$db = new DB();
				$user = $db->get($sql);
				echo "<pre>";
				var_dump($user);
				if (empty($user)) {
					echo "该用户不存在或者用户密码错误！";
				} else {
					//echo $user[0][1]."用户已登录！";	
					echo session_id();
					$_SESSION['name'] = $username;
					$_SESSION['password'] = $pass;
					$_SESSION['id'] = $user[0][0];
					header("location:administer-user.php");
				}
		}
	}

 ?>